<?php
namespace waf;

class AutoLoader {

    private static $loader;

    public static function init($autoload = '') 
    {
        if(!$autoload)
            $autoload = WAF_LIB_PATH . '/vendor/autoload.php';

        if (static::$loader == null) {
            static::$loader = require_once $autoload;
        }
    }

    public static function addNamespacePsr4Dir($data)
    {
        if($data && is_array($data))
        {
            foreach($data as $namespace => $path)
            {
                static::$loader->setPsr4($namespace, $path);
            }
        }
    }

    public static function addClassMapDir($dir) 
    {
        static::$loader->addClassMap(static::generateClassMapFiles(new \RecursiveDirectoryIterator($dir)));
    }

    public static function addRequireFiles(array $files) 
    {
        foreach ($files as $file) {
            require_once $file;
        }
    }

    public static function getClassMap() 
    {
        return self::$loader ->getClassMap();
    }

    public static function getNamespacePsr4Map()
    {
        return self::$loader->getPrefixesPsr4();
    }

    private static function generateClassMapFiles($dir) 
    {

        $files = array();

        for (; $dir->valid(); $dir->next()) {
            if ($dir->isDir() && !$dir->isDot()) {
                if ($dir->haschildren()) {
                    $files = array_merge($files, static::generateClassMapFiles($dir->getChildren()));
                };
            }else if($dir->isFile()){
                $files[$dir -> getBasename(".php")] = $dir->getPathName();
            }
        }
        return $files;
    }
}
?>
